package me.rocky.controller;

import me.rocky.component.JWTUtil;
import me.rocky.domain.AuthRequest;
import me.rocky.domain.AuthResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

/**
 * @author Rocky
 * @version 1.0
 * @description
 * @email inaho00@foxmail.com
 * @createDate 2021/9/22 23:43
 * @log
 */
@RestController
public class LoginController {

	@Autowired
	private UserDetailsService userDetailsService;
	@Autowired
	private JWTUtil jwtUtil;

	@PostMapping("/login")
	public Mono<ResponseEntity<AuthResponse>> login(@RequestBody AuthRequest ar) {
		UserDetails user = userDetailsService.loadUserByUsername(ar.getUsername());
		return Mono.just(ResponseEntity.ok(new AuthResponse(jwtUtil.generateToken(user))));
	}


}
